//
//  NRWCarChooseTableVC.m
//  New_Runway
//
//  Created by NewLife on 2018/3/7.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "NRWCarChooseTableVC.h"
#import "CrmCustomerLevelModel.h"
#import "CRMCustomerChannelModel.h"
#import "NRWCarMileageModel.h"
#import "NRWCarAgeModel.h"

@interface NRWCarChooseTableVC ()<UITableViewDelegate, UITableViewDataSource>

@end

@implementation NRWCarChooseTableVC
{
    UITableView *tableView;
    NSArray <CrmCustomerLevelModel *>*levelModel;
    NSArray <CRMCustomerChannelModel *>*channelModel;
    NRWCarAgeModel *ageModel;
    NRWCarMileageModel *mileageModel;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if (self.chooseType == NRWCarChooseTableVCTypeChannel) {
        
        self.navigationBarView.titleLabel.text = @"客户来源";
        [self loadCustomerChannelData];
    } else if (self.chooseType == NRWCarChooseTableVCTypeLevel) {
        
        self.navigationBarView.titleLabel.text = @"客户等级";
        [self loadCustomerLevelData];
    }
    
    [self setupUI];
}

- (void)setupUI {
    
    tableView = DPTableViewForStyle(UITableViewStylePlain)
    .setDelegate(self)
    .setDataSource(self)
    .registerCell(UITableViewCell.class, @"cell")
    .setTableFooterView(DPView())
    .setFrame(CGRectMake(0, self.navH, Screen_width, Screen_height - self.navH));
    
    self.view.addSubview(tableView);
}

- (void)rightItemClick {
    
    [self turnToFromVC];
}

- (void)loadCustomerChannelData {

    [SVProgressHUD show];
    
    NSDictionary *dict = @{@"marketId":[NRWCarUser sharedUser].marketId,
                           @"shopId": [NRWCarUser sharedUser].shopId,
                           @"channelState": @1
                           };
    
    [YQNetworking postWithUrl:[NSString stringWithFormat:@"%@%@",CRMContextApi, customersApi] refreshRequest:NO cache:NO params:dict progressBlock:nil successBlock:^(id response) {
        
        NSMutableArray *arrM = @[].mutableCopy;
        
        if ([response[@"code"] integerValue] == 1) {
            
            NSArray *arr = response[@"result"];
            
            for (NSDictionary *dict in arr) {
                
                CRMCustomerChannelModel *model = [CRMCustomerChannelModel mj_objectWithKeyValues:dict];
                [arrM addObject:model];
            }
            channelModel = arrM.copy;
            [tableView reloadData];
        }
        
        [SVProgressHUD dismiss];
        
    } failBlock:^(NSError *error) {
        [ShowMessage showHintMessage:@"请求失败!" duration:2.0 withRemindId:remindFail];
        [SVProgressHUD dismiss];
    }];
}

- (void)loadCustomerLevelData {
    
    [SVProgressHUD show];
    
    NSDictionary *dict = @{@"marketId":[NRWCarUser sharedUser].marketId,
                           @"shopId": [NRWCarUser sharedUser].shopId,
                           @"levelState": @1
                           };
    
    [YQNetworking postWithUrl:[NSString stringWithFormat:@"%@%@",CRMContextApi, customerLevelApi] refreshRequest:NO cache:NO params:dict progressBlock:nil successBlock:^(id response) {
        NSMutableArray *arrM = @[].mutableCopy;
      
        if ([response[@"code"] integerValue] == 1) {
            
            NSArray *arr = response[@"result"];
            
            for (NSDictionary *dict in arr) {
                
                CrmCustomerLevelModel *model = [CrmCustomerLevelModel mj_objectWithKeyValues:dict];
                [arrM addObject:model];
            }
            levelModel = arrM.copy;
            [tableView reloadData];
        }

        [SVProgressHUD dismiss];
        
    } failBlock:^(NSError *error) {
        [ShowMessage showHintMessage:@"请求失败!" duration:2.0 withRemindId:remindFail];
        [SVProgressHUD dismiss];
    }];
}

- (void)turnToFromVC {
    
    if (self.fromClass == nil) {
        [self.navigationController popViewControllerAnimated:YES];
    }
    
    for (NSInteger i = self.navigationController.viewControllers.count - 1; i >= 0; i--) {
        UIViewController *vc = self.navigationController.viewControllers[i];
        if ([vc isKindOfClass:self.fromClass]) {
            
            [self.navigationController popToViewController:vc animated:YES];
            break;
        }
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (self.chooseType == NRWCarChooseTableVCTypeChannel) {
        
        return channelModel.count;
    } else if (self.chooseType == NRWCarChooseTableVCTypeLevel) {
        
        return levelModel.count;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    cell.separatorInset = UIEdgeInsetsZero;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    if (self.chooseType == NRWCarChooseTableVCTypeChannel) {
        
        cell.textLabel.text = channelModel[indexPath.row].channelName;
    } else if (self.chooseType == NRWCarChooseTableVCTypeLevel) {
        
        cell.textLabel.text = levelModel[indexPath.row].levelTxt;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString *itemId = nil;
    NSString *itemName = nil;
    
    if (self.chooseType == NRWCarChooseTableVCTypeChannel) {
        
        itemId = channelModel[indexPath.row].channelId;
        itemName = channelModel[indexPath.row].channelName;
    } else if (self.chooseType == NRWCarChooseTableVCTypeLevel) {
        
        itemId = levelModel[indexPath.row].levelId;
        itemName = levelModel[indexPath.row].levelTxt;
    } 
    
    if (self.chooseCompleted) {
        self.chooseCompleted(itemId, itemName);
    }
    
    [self turnToFromVC];
}

@end
